<?php
class Access {
    public $CI;
    public function __construct() {
        $this->CI =& get_instance();
    }
    public function start() {
        $this->CI->load->library( 'zend' );
        $this->CI->zend->load( 'Zend_Acl' );
        $this->setRoles();
        $this->setResources();
        $this->setAccess();
        $module = $this->CI->router->fetch_module();
        $controller = $this->CI->router->fetch_class();
        $action = $this->CI->router->fetch_method();
        //$level = $this->CI->session->userdata('ses_level');
        $role = 'guest';
        // switch($level){
        //     case '1':
        //         $role = 'admin';
        //         break;
        //     case '2':
        //         $role = 'superadmin';
        //         break;
        // }
        if ( !$this->CI->Zend_Acl->isAllowed( $role, $module . ':' . $controller, $action ) ) {
            redirect( base_url() . 'auth/login' );
        }
    }
    public function setRoles() {
        $this->CI->Zend_Acl ->addRole( new Zend_Acl_Role( 'guest' ) )
        ->addRole( new Zend_Acl_Role( 'admin','guest' ) )
        ->addRole( new Zend_Acl_Role( 'superadmin' ), 'admin' );
    }
    public function setResources() {
        $this->CI->Zend_Acl ->add( new Zend_Acl_Resource( 'home' ) )->add( new Zend_Acl_Resource( 'home:home','home' ) )
                            ->add( new Zend_Acl_Resource( 'auth' ) )
                            ->add( new Zend_Acl_Resource( 'auth:auth') )
                            // ->add( new Zend_Acl_Resource( 'auth:auth:login','auth' ) )
                            ->add( new Zend_Acl_Resource( 'admin' ) )
                            ->add( new Zend_Acl_Resource( 'clientInfo' ) );
    }
    public function setAccess() {
        $this->CI->Zend_Acl ->allow( 'guest', array( 'home' ) )->allow( 'admin', array( 'admin', 'clientInfo' ) )
        ->allow( 'superadmin', null );
    }
}
